<template>
  <div class="mainos">
    <div class="main_spac1 paddingBottom5">
      <div class="main_white padding10 paddingBottom20">
        <div class="main_crumbs boxdq">
          <div class="main_crumbs_l">
            <a href="javascript:;" @click="$router.back(-1)">
              <i class="el-icon-arrow-down"></i>返回
            </a>
            <em>|</em>
            <span>查看</span>
          </div>
          <div class="main_crumbs_rt">
            <el-button v-repeatClick class="marginLeft5" @click="print"
              >打印</el-button
            >
            <el-button v-repeatClick @click="exportConfirm" class="marginLeft5"
              >导出</el-button
            >
          </div>
        </div>
        <div class="main_tile boxdq marginTop10">
          <div class="main_tile_l marginLeft0"><em></em>基础信息</div>
        </div>
        <div class="main_xq">
          <ul class="boxha">
            <li>
              结算单号：<span>{{ info.info.order_sn }}</span>
            </li>
            <li>
              结算日期：<span>{{
                parseTime(info.info.created_at, "{y}-{m}-{d}")
              }}</span>
            </li>
            <li>
              采购方式：<span>{{ info.modeList[info.info.mode] }}</span>
            </li>
            <li>
              采购员/供应商：<span>{{
                info.info.mode == 1
                  ? info.buyer[info.info.buyer_id]
                  : info.supplier[info.info.supplier_id]
              }}</span>
            </li>
            <li>
              账期：<span>
                {{ info.info.payment_days }}
              </span>
            </li>
            <li>
              本次付款金额：<span>{{ info.info.paid }}</span>
            </li>
            <li>
              本次抹零金额：<span>{{ info.info.zero_money }}</span>
            </li>
            <li>
              付款方式：<span>{{
                info.pay_type_list[info.info.pay_type]
              }}</span>
            </li>
            <li>
              收款人：<span>{{ info.info.payee }}</span>
            </li>
            <li class="main_w100">
              备注：<span>{{ info.info.remark }}</span>
            </li>
          </ul>
        </div>
        <div class="main_tile boxdq marginTop10">
          <div class="main_tile_l marginLeft0"><em></em>单据列表</div>
          <div class="mainos_tile_r">合计：{{ info.info.paid }}元</div>
        </div>
        <div class="main_table marginTop0">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="info.info.advice_settlement_list"
            element-loading-text="加载中"
            fit
            highlight-current-row
            show-summary
            :summary-method="getSummaries"
          >
            <el-table-column label="序号" prop="userId">
              <template slot-scope="scope">{{ scope.$index + 1 }}</template>
            </el-table-column>
            <el-table-column
              label="账单号"
              width="200"
              prop="related_purchase_order_sn"
            />
            <el-table-column label="单据类型" prop="status">
              <template slot-scope="scope">{{
                info.typeList[scope.row.type]
              }}</template>
            </el-table-column>
            <el-table-column label="应付金额" prop="money" />
            <el-table-column label="已付金额" prop="paid" />
            <el-table-column label="抹零金额" prop="pre_zero_amount" />
            <el-table-column label="未付金额" prop="unpaid" />
            <el-table-column label="本次付款金额" prop="pay_money" />
            <el-table-column label="本次抹零金额" prop="zero_amount" />
            <el-table-column label="剩余未付" prop="out_pay" />
            <el-table-column label="备注" prop="remark" class-name="main_hei" />
          </el-table>
        </div>
        <div class="main_tile boxdq marginTop10">
          <div class="main_tile_l marginLeft0"><em></em>其他</div>
        </div>
        <div class="main_xq">
          <ul class="boxha">
            <li class="main_w100 box marginTop20">
              <span style="flex-shrink: 0">显示相关凭证：</span>
              <div class="box main_xqi">
                <div
                  class="main_xqi_i marginRight10"
                  v-for="(pic, index) in pics"
                  :key="index"
                >
                  <label class="el-upload-list__item-status-label">
                    <i class="el-icon-upload-success el-icon-check"></i>
                  </label>
                  <el-image
                    fit="fill"
                    :src="pic"
                    :preview-src-list="pics"
                    style="width: 200px"
                  >
                    <div slot="error" class="image-slot">
                      <img :src="puzzleImg" />
                    </div>
                  </el-image>
                </div>
              </div>
            </li>
          </ul>
        </div>
      </div>
      <!-- 打印子组件 -->
      <order-print
        ref="printTemplate"
        v-show="false"
        id="printname"
      ></order-print>
    </div>
  </div>
</template>

<script>
import {
  advice_info,
  export_advice_detailList,
} from "@/api/financial/financialRed";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { parseTime } from "@/utils/index";
import OrderPrint from "@/views/order/orderPrint.vue";
import { puzzleImg } from "@/api/currency";
import { mapGetters } from "vuex";

export default {
  components: { OrderPrint },
  computed: {
    ...mapGetters(["avatar"]),
    total() {
      let sz = 0;
      this.info.info.advice_settlement_list.forEach((e) => {
        sz += parseFloat(this.$toFixed(parseFloat(e.paid)));
      });
      return this.$toFixed(sz);
    },
  },

  data() {
    return {
      // 时间
      parseTime,
      puzzleImg,
      // 基本信息
      info: {
        info: {
          money: 0,
          created_at: "2000-01-01",
          type: 0,
          status: 0,
          mode: 0,
          advice_settlement_list: [],
        },
        buyer: ["未分拣"],
        modeList: ["未分拣"],
        supplier: ["未分拣"],
        typeList: ["未分拣"],
        pay_type_list: ["未分拣"],
        statusList: ["未分拣"],
      },
      logs: [],
      base: {},
      pics: [],
      listLoading: false,
      // id
      id: this.$route.params.id,
    };
  },
  created() {
    // 基本信息
    this.advice_info();
  },
  methods: {
    // 基本信息
    advice_info() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, { id: this.id })) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: this.id }
      );
      advice_info(data).then((res) => {
        if (res.errcode == 0) {
          res.data.logs.map((e) => {
            if (e.content) {
              e.content = JSON.parse(e.content);
            }
          });
          let arr = [];
          let list = [];
          if (res.data.info.pics) {
            arr = res.data.info.pics.split(",");
            list = arr.map((e) => {
              return this.avatar.domain + e;
            });
          }

          this.pics = list;
          this.info = res.data;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 导出
    exportConfirm() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        {
          id: this.id,
        }
      );
      export_advice_detailList(data)
        .then((res) => {
          const link = document.createElement("a");
          // let blob = new Blob([res], { type: "application/vnd.ms-excel" });
          let blob = new Blob([res], {
            type:
              "application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
          });
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);
          link.download = "结算列表详情";
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        })
        .catch(() => {
          this.$message.error("导出失败");
        });
    },
    // 打印
    print() {
      let id = this.id;
      this.$refs.printTemplate.$emit("demo", { id: id, type: "advice" });
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        switch (index) {
          case 0:
            sums[index] = "合计";
            break;
          case 7:
            sums[index] = this.$toFixed(parseFloat(this.info.info.paid));
            break;
        }
      });

      return sums;
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainos {
  .main_tile {
    .mainos_tile_r {
      color: $colour_red;
      font-size: $font_14;
    }
  }
  .mainor_ex {
    position: fixed;
    width: 220px;
    height: 100%;
    background: $white;
    box-shadow: 0 6px 10px rgba(0, 21, 41, 0.08);
    top: 0;
    right: 0;
    z-index: 9999;
    transition: all 0.5s;
    transform: translate3d(100%, 0, 0);
    &.on {
      transform: translate3d(0, 0, 0);
    }
    .mainor_exb {
      position: absolute;
      width: 100%;
      height: calc(100% - 60px);
      overflow-y: auto;
      h3 {
        font-size: $font_16;
        color: $colour_18;
        height: 50px;
        line-height: 50px;
        border-bottom: 1px solid $colour_e6;
        padding-left: 20px;
      }
      .mainor_exb_an {
        padding-left: 20px;
        margin-top: 15px;
        /deep/.el-checkbox {
          display: block;
          margin-top: 15px;
          margin-bottom: 15px;
        }
      }
    }
    .mainor_exb_b {
      position: absolute;
      bottom: 0;
      width: 100%;
      border-top: 1px solid $colour_e6;
      height: 55px;
      text-align: center;
      /deep/.el-button--default {
        margin-top: 12px;
        border-color: $subActive;
        color: $menuText;
        &:hover {
          border-color: $menuActiveText;
          color: $menuActiveText;
        }
      }
    }
  }
}
</style>
